import dateDiff  from '../../../util/dateDiff'
import ChatRecordMessageModule from '../../../models/3.0/chat'
const app = getApp()
var websocket = require('../../../util/websocket');


Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		newslist: [],
		userInfo: {},
		scrollTop: 0,
		aniStyle: true, //动画效果
		content: "",
		previewImgList: [],
		toUser:'',
		toUserId:'',
		mychat:[],
		yourchat:[],
		count:15
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
	var that = this
    that.setData({
		userInfo : wx.getStorageSync('userInfo'),
		toUser:options.toUser,
		toUserId:options.toUserId
    })
		// const userInfo = wx.getStorageSync('userInfo')
		console.log(this.data.userInfo)
		console.log("to",this.data.toUser)
	//调通接口
	websocket.connect(this.data.userInfo, function (res) {
		console.log(JSON.parse("res",res.data))
		var list = []
		list = that.data.newslist
		list.push(res.data)
		that.setData({
		newslist: list
		})
	})

	this.ChatRecordMessageModule()

	},
	
	send: function() {
		var flag = this
		if (this.data.content.trim() == "") {
			wx.showToast({
				title: '消息不能为空哦~',
				icon: "none",
				duration: 2000
			})
		} else {
			setTimeout(function() {
				flag.setData({
					increase: false
				})
			}, 500)
			websocket.send('{ "content": "' + this.data.content + '","msgType":"text", "senderName": "' + this.data.userInfo.nick_name  + '", "avatarUrl": "' + this.data.userInfo.avatar + '", "senderId": "' + this.data.userInfo.user_id + '" , "receiverName": "' + this.data.toUser + '" , "receiverId": "' + this.data.toUserId + '", "sendDate": "' + Date.parse(new Date()) + '" }').then(res=>{
				 
				wx.onSocketMessage((res)=>{ 
					console.log(res.data)
					var list = []
					list = flag.data.newslist
					list.push(JSON.parse(res.data))
					flag.setData({
					newslist: list
					})
					console.log("newslistssss",flag.data.newslist)
					}); 
					

			})

				this.bottom()
		} 
		this.setData({
			content: ''
		})
	},


	

	yourChatRecordMessageModule(){
	
			let params={
				receiverId:this.data.toUserId,
				 senderId:wx.getStorageSync('userInfo').user_id,
				page:1,
				count:this.data.count
			}
			ChatRecordMessageModule.chat(params,res=>{
				let arr = res.reverse()
				 this.setData({
					'newslist':arr
				 })
			})
	},
	


	ChatRecordMessageModule(){
		this.yourChatRecordMessageModule()	
	},


	//监听input值的改变
	bindChange(res) {
		this.setData({
			content: res.detail.value
		})
	},
	increase() {
		this.setData({
			increase: true,
			aniStyle: true
		})
	},
	//点击空白隐藏message下选框
	outbtn() {
		this.setData({
			increase: false,
			aniStyle: true
		})
	},
	//聊天消息始终显示最底端
	bottom: function() {
		var query = wx.createSelectorQuery()
		query.select('#flag').boundingClientRect()
		query.selectViewport().scrollOffset()
		query.exec(function(res) {
			wx.pageScrollTo({
				scrollTop: res[0].bottom // #the-id节点的下边界坐标
			})
			res[1].scrollTop // 显示区域的竖直滚动位置
		})
	},


	scrolly(){
		this.setData({
			count:this.data.count+5
		})
		this.yourChatRecordMessageModule()	
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		wx.closeSocket();
		wx.showToast({
			title: '连接已断开~',
			icon: "none",
			duration: 2000
		})
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
